package cn.com.pxto.servelet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PxtoServlet extends HttpServlet {
	
	private static  String url = "jdbc:mysql://127.0.0.1:3306/pxto?useUnicode=true&amp;characterEncoding=UTF-8";
	private static  String username = "root";
	private static  String password = "root";
	private static  String driver = "com.mysql.jdbc.Driver";
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			this.service(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			this.service(req, resp);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//			System.out.println(req.getParameter("cmdId"));
//			Integer cmdId=new Integer(1);
//			switch (cmdId) {
//			case 1:
//				School newSchool =new School();
//				newSchool.setSid(new Integer(1));
//				newSchool.setName("荆门市第一中学");
//				newSchool.setLessonTypeStr("2;3;4;5;6;");
//				req.setAttribute("school", newSchool);
//				break;
//			case 2:
//				break;
//			default:
//				break;
//			}
//			System.out.println("success in server!");
		Connection conn = null;
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url,username,password);
		}catch(Exception e){
			e.printStackTrace();
		}
		if(conn!=null){
			System.out.println("建立连接成功!");
			Statement stat;
			try {
				stat = conn.createStatement();
				String tel=req.getParameter("tel");
				ResultSet result = stat.executeQuery("SELECT * FROM tbl_guest where tel like '%"+tel+"%'");
				System.out.println("SQL:"+"SELECT * FROM tbl_guest where tel like '%"+tel+"%'");
				System.out.println("----------------------------------------------------------");
				int i = 0;
				while (result.next()) {
					i++;
					System.out.println(String.valueOf(i) + "  " + result.getString(1) + "  " + result.getString(2) + "  " + result.getString(3));
				}
				System.out.println("----------------------------------------------------------");
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		
	}

}
